import pygame
from pygame.sprite import *
import math
import time

python_players = {}

# when there's nothing in front of the player
PLAYER_MOVE = 1
# when infront there are enemy players
PLAYER_ATTACK = 2
# when infront there are more players
PLAYER_WAIT = 3

PLAYER_ATTACK_AND_MOVE = 4

troops_info = {
                "dino_player" : { "power" : 12, "power_width":60, "strength" : 2, "initial_position":(90,325)},
                "slinger_player" : {"power" : 4, "collision_distance": 40, "power_width":30 , "strength":1, "initial_position":(90,350) },
                "club_man_player" : { "power" : 4, "power_width":30, "strength":1, "initial_position":(90,350) },
                "my_evolution1_castle" : { "power" : 3000 , "strength" : 0, "initial_position":(0,285) },
                "enemy_evolution1_castle" : { "power" : 3000, "strength" : 0, "initial_position":(871,286) }}

class Powers:
    powers = None

    def __init__( self ):
        self.powers = []

    def add( self, power ):
        self.powers.append( power)

    def draw( self, screen ):
        for p in self.powers:
            p.draw( screen )



class Power:
    height = 10
    power = None
    full_power = None
    power_width = None
    sprite = None

    def __init__(self, power, sprite ):
        self.power = power
        self.full_power = power # in the beginning a troop has full power.
        self.sprite = sprite
        self.power_width = 100

    def draw( self, screen ):
        if self.power > 0 :
            pygame.draw.rect(screen, (255,0,0), (self.sprite.rect.left,self.sprite.rect.top - 10.,(self.power/self.full_power)*self.power_width,10))
            font = pygame.font.Font(None, 14)
            text = font.render( str(self.power) + "/" + str(self.full_power), 1, (255,255,255))
            screen.blit(text, (self.sprite.rect.x, self.sprite.rect.y - 10))



class Troop(pygame.sprite.Sprite):
    power = None
    key = None
    strength = None
    def __init__(self, key ):
        Sprite.__init__(self)
        self.key = key
        if not hasattr(python_players,self.key) :
            print("creating " + key )
            python_players[key] = pygame.image.load("graphics/" + key + ".png")
        self.image = python_players[key]
        self.rect = self.image.get_rect()
        if "initial_position" in troops_info[key]:
            self.rect = self.rect.move(troops_info[key]["initial_position"])
        if "power" in troops_info[key]:
            self.power = Power(troops_info[key]["power"],self)
            if "power_width" in troops_info[key]:
                self.power.power_width = troops_info[key]["power_width"]
        if "strength" in troops_info[key]:
            self.strength = troops_info[key]["strength"]

    def update(self):
        pass

    def draw(self,surface):
        Sprite.draw(self,surface)
        print(" I am drawing")
        # testing "power" display with rect
        pygame.draw.rect(self.screen, (255,0,0), (self.left,self.top-10,self.power,10))

    def is_dead(self):
        return self.power == 0
        
    def get_hit(self,hit_power):
        self.power.power -= hit_power
        if self.power.power < 0:
            self.power.power = 0

class Player(Troop):

    action = PLAYER_MOVE
    creation_time = None

    def __init__(self, key ):
        Troop.__init__(self,key + "_player")
        self.creation_time = int(round(time.time() * 1000))

    def attack(self,other):
        print("player " + self.key + " is attacking : " + other.key )
        self.action = PLAYER_ATTACK
        if self.is_can_move_on_attack(other):
            self.action = PLAYER_ATTACK_AND_MOVE
        other.get_hit(self.strength)

    # can my troop move if it collided with the enemy's
    def is_can_move_on_attack(self, other):
        return "collision_distance" in troops_info[self.key] and  math.fabs(self.rect.left - other.rect.left) > troops_info[self.key]["collision_distance"]

    # can my troop move if it collides with another troop
    def is_can_move_on_self_collide(self, other):
        return other.is_before(self) and ( "collision_distable" not in troops_info[self.key] or self.is_can_move_on_attack(other) )

    def wait(self):
        self.action = PLAYER_WAIT

    def is_before(self, other):
        return self.creation_time < other.creation_time

    def update(self):
        if self.action == PLAYER_MOVE or self.action == PLAYER_ATTACK_AND_MOVE :
            self.rect = self.rect.move((1,0))
        self.action = PLAYER_MOVE
        


            

        

